
import rcontrolutil
import rcontrol
import Tkinter



class RButton(rcontrol.RControl):
	"""
	Properties:
	
	*text* - Text of the button. The & character will be shown as an alt-key.
	*icon_position* -  Use one of 'center','left','right','top','bottom'.
	*onclick* - Function. Command that is called upon click. None represents nothing bound.
	*enabled* - True or False. If False, the button is disabled.
	*height* - Integer, where one unit is the height of a line of text.
	*width* - Integer, where one unit is the width of a char.
	*borderstyle* - Border style. One of 'sunken','raised','groove','ridge','flat'. Default is 'raised'. Set to 'sunken' and it will appear pressed.
	*defaultstyle* - Set to True to draw a border indicating this is the default button.
	*borderwidth* - Integer, defaults to 1 or 2.
	*font* - Set font of button text. Standard tkinter font format, such as ('Times New Roman', 12, 'normal')
	*anchor* - Set where in the button the text (or image) should be located. Provide one of the strings 'center','top','topright','right','bottomright','bottom','bottomleft','left','topleft'. Default is center.
	*bgcolor* - Background color of the button. RGB tuple, such as (255,0,0) for pure red.
	*color* - Foreground color of the button, to use for text. RGB tuple.
	*wrapwidth* - Set when a button's text should be wrapped into multiple lines, in screen units. Default is 0 (no wrapping).
	*activebgcolor* - Background color of the button when the button is active. RGB tuple.
	*activecolor* - Foreground color of the button when the button is active. RGB tuple.
	*alignment* - Set alignment of text when there are multiple lines. One of 'left','right','center'.
	"""
	
	_basecontrol = None
	
	_iconPos = Tkinter.LEFT
	_onlyIcon = False
	
	def __init__(self, strLabel=None, fnCallback=None, parent=None, **options):
		self._newWidgetManual(parent, Tkinter.Button)
		
		if strLabel!=None: self.text = strLabel
		if fnCallback!=None: self.onclick=fnCallback
		self.set_many(**options)
	
	def call(self):
		"""Call the callback associated with the button."""
		self._basecontrol.invoke()
	
	
	def set_icon(self, filename):
		if not filename:
			#show no image
			self._basecontrol.image = None
			self._basecontrol.config(image='')
		else:
			fnamelower = filename.lower()
			if not (fnamelower.endswith('.gif') or fnamelower.endswith('.pgm') or fnamelower.endswith('.ppm')):
				raise rcontrolutil.RException("Only images of format .gif, .pgm, or .ppm are supported.")
			image = Tkinter.PhotoImage(file=filename)
			self._basecontrol.image = image #store a reference to image! important!
			self._basecontrol.config(image=image)
			self.show_only_icon(False) #make sure that the label is shown as well
			
		
	def set_icon_base64(self, data):
		'''data must be a base-64 encoded GIF'''
		if not data:
			#show no image
			self._basecontrol.image = None
			self._basecontrol.config(image='')
		else:
			image = Tkinter.PhotoImage(format='gif', data=data)
			self._basecontrol.image = image #store a reference to image! important!
			self._basecontrol.config(image=image)
			self.show_only_icon(False) #make sure that the label is shown as well
	
	def show_only_icon(self, bShowOnlyIcon=True):
		'''Shows only the icon, creating a bitmap button.'''
		if bShowOnlyIcon:
			self._basecontrol['compound'] = Tkinter.NONE
		else:
			self._basecontrol['compound'] = self._iconPos
		self._onlyIcon = bShowOnlyIcon
	
	def property_icon_position_get(self):
		return rcontrolutil.mapPosition.lookupForward(self._iconPos)
	def property_icon_position_set(self, value):
		self._iconPos = rcontrolutil.mapPosition.lookupReverse(value)
		if self._onlyIcon:
			self._basecontrol['compound'] = Tkinter.NONE
		else:
			self._basecontrol['compound'] = self._iconPos
	
	
	#In tkinter, if you set the 'command' to None, it doesn't remove the binding. this fixes that.
	def property_onclick_get(self): #convert '' or 0 to None
		if not self._basecontrol['command']: return None
		else: return self._basecontrol['command']
	def property_onclick_set(self, value):
		if value: self._basecontrol['command'] = value
		else: self._basecontrol['command'] = ''
	
	def property_text_get(self):
		return rcontrolutil.underlinePositionToString(
			self._basecontrol['text'], self._basecontrol['underline'])
	
	# If given a string '&hello', underline the h.
	def property_text_set(self,value):
		text, nIndex = rcontrolutil.stringToUnderlinePosition(value) 
		self._basecontrol.config(text=text,underline=nIndex)
		
	def property_enabled_get(self):
		if self._basecontrol['state']==Tkinter.DISABLED: return False
		else: return True

	def property_enabled_set(self, value):
		if value: self._basecontrol['state'] = Tkinter.NORMAL
		else: self._basecontrol['state'] = Tkinter.DISABLED
			
	def property_defaultstyle_get(self):
		if self._basecontrol['default']==Tkinter.DISABLED: return False
		else: return True
		
	def property_defaultstyle_set(self, value):
		if value: self._basecontrol['default'] = Tkinter.ACTIVE
		else: self._basecontrol['default'] = Tkinter.DISABLED
		
	


	#as described in internal_doc.rtf
	_properties = {
	
		'text': (property_text_get, property_text_set),
		
		'icon_position': (property_icon_position_get, property_icon_position_set),
	
		'onclick': (property_onclick_get, property_onclick_set),
		'enabled': (property_enabled_get, property_enabled_set),
		'width': 'width',
		'height': 'height',
		
		'borderwidth': 'borderwidth',
		'defaultstyle': (property_defaultstyle_get,property_defaultstyle_set),
		'borderstyle': ('relief',rcontrolutil.mapBorderstyle),
		'font': 'font',
		'wrapwidth': 'wraplength',
		'anchor': ('anchor', rcontrolutil.mapAnchor),
		'alignment': ('justify', rcontrolutil.mapAlignment),
		'bgcolor': ('background',rcontrolutil.mapColor),
		'color': ('foreground',rcontrolutil.mapColor),
		'activebgcolor': ('activebackground',rcontrolutil.mapColor),
		'activecolor': ('activeforeground',rcontrolutil.mapColor),
	
	
		}




